package com.whlilong.jxc.sys;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class CacheImage {
	private final Map<String, Image> imageMap = new HashMap<String, Image>();
	private static CacheImage INSTANCE = new CacheImage();

	private CacheImage() {

	}

	public static CacheImage getINSTANCE() {
		return INSTANCE;
	}

	public Image getImage(String applicationID, String imageName) {
		if (imageName == null) {
			return null;
		}
		Image image = (Image) imageMap.get(imageName);
		if (image == null) {
			image = AbstractUIPlugin.imageDescriptorFromPlugin(applicationID,
					imageName).createImage();
			imageMap.put(imageName, image);

		}
		return image;
	}
	public void dispose(){
		Iterator iterator = imageMap.values().iterator();
		while(iterator.hasNext()){
			((Image)iterator.next()).dispose();
			imageMap.clear();
		}
	}

}
